﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FMSpaceShooter.Screens;
using FMSpaceShooter.ScreenManager;
using FMSpaceShooter.GameObjects.ItemFolder;

namespace FMSpaceShooter.GameObjects
{
    class CantCatchMeEnemy : EnemyBase
    {
        public new const string TypeName = "CantCatchMeEnemy";
        public const string imageName = "Images/Enemies/firstEnemy";

        private Vector2 runVelocity = Vector2.Zero;
        private const float runAccel = 500f;
        public override int Points { get { return 20; } }

        public CantCatchMeEnemy(GameplayScreen screen, Vector2 position)
            :base(screen, imageName, position, 30)
        {

        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);


            if (screen.GOM.PlayerOne.IsInPlay)
            {
                Vector2 delta = (position - screen.GOM.PlayerOne.Position);
                delta.Normalize();

                float deltaRotation = Util.Tools.VectorToAngle(delta);
                float diff = MathHelper.WrapAngle(deltaRotation - screen.GOM.PlayerOne.Rotation);
                    var direction = screen.GOM.PlayerOne.Position - position;

                if (Math.Abs(diff) > (Math.PI / 8))
                {
                    direction.Normalize();

                    float strength = 1;

                    if (Math.Abs(diff) > (Math.PI / 2))
                    {
                        strength = 2.8f;
                    }

                    position += (direction * (float)(gameTime.ElapsedGameTime.TotalSeconds * 100)) * strength;
                }
                else
                {
                    float strength = 1 / Vector2.Distance(position, screen.GOM.PlayerOne.Position) + 0.5f;
 
                    if (diff > 0)
                    {
                        runVelocity += Util.Tools.AngleToVector((float)(deltaRotation + (Math.PI / 2))) * runAccel * strength;
                    }
                    else
                    {
                        runVelocity += Util.Tools.AngleToVector((float)(deltaRotation - (Math.PI / 2))) * runAccel * strength;
                    }
                }
                // runVelocity is the problem USE 0.016667 CONSTANT instead of gameTime.ElapsedGameTime.TotalSeconds  .66646667
                runVelocity *= .6f;//0.9f * (float)(0.01111 / gameTime.ElapsedGameTime.TotalSeconds);
                position += runVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

                rotation = Util.Tools.VectorToAngle(direction);
            }
            
            Vector2 res = new Vector2(screen.GraphicsDeviceManager.PreferredBackBufferWidth,
                screen.GraphicsDeviceManager.PreferredBackBufferHeight);

            if (position.X < 0)
            {
                position.X = 0;
            }
            else if (position.X > res.X)
            {
                position.X = res.X;
            }
            if (position.Y < 0)
            {
                position.Y = 0;
            }
            else if (position.Y > res.Y)
            {
                position.Y = res.Y;
            }
        }

        public override void Die()
        {
            base.Die();
            int x = screen.Random.Next(20);

            if (x < 2)
            {
                screen.GOM.AddToPlay(new PrimaryBulletBoost(screen, this.position));
            }
            else if (x == 7)
            {
                screen.GOM.AddToPlay(new SplitterBulletBoost(screen, this.position));
            }

        }

    }
}
